package org.lql.algo.codecrush.hot100.matrix;

import java.util.Arrays;

/**
 * @author: liangqinglong
 * @date: 2025-10-11 17:41
 * @description: 73. 矩阵置零 <a href="https://leetcode.cn/problems/set-matrix-zeroes/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class SetZeroes {

	public void setZeroes(int[][] matrix) {
		if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
			return;
		}
		int m = matrix.length;
		int n = matrix[0].length;
		boolean[] row = new boolean[m];
		boolean[] col = new boolean[n];
		// tag
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (matrix[i][j] == 0) {
					row[i] = true;
					col[j] = true;
				}
			}
		}
		// set
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (row[i] || col[j]) {
					matrix[i][j] = 0;
				}
			}
		}
	}

	public static void main(String[] args) {
		SetZeroes setZeroes = new SetZeroes();
		int[][] matrix = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
		setZeroes.setZeroes(matrix);
		System.out.println(Arrays.deepToString(matrix));
		System.out.println();
		matrix = new int[][]{{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
		setZeroes.setZeroes(matrix);
		System.out.println(Arrays.deepToString(matrix));
	}
}
